package org.pt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.pt.model.Comment;
import org.pt.model.Post;
import org.pt.service.SocialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/social")
public class SocialController {

    @Autowired
    private SocialService socialService;

    // 发帖相关接口
    @PostMapping("/posts")
    public ResponseEntity<Post> createPost(@RequestBody Post post) {
        return ResponseEntity.ok(socialService.createPost(post));
    }

    @GetMapping("/posts")
    public ResponseEntity<IPage<Post>> getPosts(
            @RequestParam(required = false) Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ResponseEntity.ok(socialService.getPosts(userId, page, size));
    }

    @GetMapping("/posts/{postId}")
    public ResponseEntity<Post> getPost(@PathVariable Long postId) {
        return ResponseEntity.ok(socialService.getPostById(postId));
    }

    // 评论相关接口
    @PostMapping("/comments")
    public ResponseEntity<Comment> createComment(@RequestBody Comment comment) {
        return ResponseEntity.ok(socialService.createComment(comment));
    }

    @GetMapping("/posts/{postId}/comments")
    public ResponseEntity<IPage<Comment>> getComments(
            @PathVariable Long postId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ResponseEntity.ok(socialService.getCommentsByPostId(postId, page, size));
    }

    // 点赞相关接口
    @PostMapping("/posts/{postId}/like")
    public ResponseEntity<Void> likePost(
            @RequestParam Long userId,
            @PathVariable Long postId) {
        socialService.likePost(userId, postId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/posts/{postId}/like")
    public ResponseEntity<Void> unlikePost(
            @RequestParam Long userId,
            @PathVariable Long postId) {
        socialService.unlikePost(userId, postId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/posts/{postId}/hasLiked")
    public ResponseEntity<Boolean> hasLiked(
            @RequestParam Long userId,
            @PathVariable Long postId) {
        return ResponseEntity.ok(socialService.hasLiked(userId, postId));
    }
    
    @GetMapping("/posts/{postId}/likeCount")
    public ResponseEntity<Integer> getLikeCount(@PathVariable Long postId) {
        return ResponseEntity.ok(socialService.getLikeCount(postId));
    }
} 